package com.jms.controller.sys;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jms.controller.base.BaseController;
import com.jms.pageModel.base.Grid;
import com.jms.pageModel.base.Json;
import com.jms.pageModel.base.PageFilter;
import com.jms.pageModel.sys.Plan;
import com.jms.service.sys.PlanServiceI;

@Controller
@RequestMapping("/plan")
public class PlanController extends BaseController{

	@Autowired
	private PlanServiceI planService;
	
	@RequestMapping("/manager")
	public String manager() {
		return "/admin/plan";
	}
	
	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(Plan plan, PageFilter ph) {
		Grid grid = new Grid();
		grid.setRows(planService.dataGrid(plan, ph));
		grid.setTotal(planService.count(plan, ph));
		return grid;
	}
	@RequestMapping("/addPage")
	public String addPage() {
		return "/admin/planAdd";
	}

	@RequestMapping("/add")
	@ResponseBody
	public Json add(Plan plan) {
		Json j = new Json();
		try {
			planService.add(plan);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(Long id) {
		Json j = new Json();
		try {
			planService.delete(id);
			j.setMsg("删除成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/get")
	@ResponseBody
	public Plan get(Long id)  {
		return planService.get(id);
	}
	
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, Long id) {
		Plan p = planService.get(id);
		request.setAttribute("plan", p);
		return "/admin/planEdit";
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(Plan plan) {
		Json j = new Json();
		try {
			planService.edit(plan);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
}
